package framework.engine;

import framework.Engine;
import framework.FileIO;
import framework.Graphics;
import framework.Input;
import framework.Screen;
import framework.graphics.canvas.CanvasGraphics;
import framework.graphics.canvas.FastRenderView;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

/**
 * A classe responsável por criar tudo  ^ cuidar do ciclo de vida do jogo.
 * As classes derivadas só precisaram implementar o método getStartScreen().
 * 
 *
 */
public abstract class AndroidEngine extends Activity implements Engine {
	FastRenderView renderView;
	Graphics graphics;

	Input input;
	FileIO fileIO;
	Screen screen;
	WakeLock wakeLock;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			Log.v("<Criação>", "Android Engine Criada");

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// deixa em fullScreen e sem barra de notificação

		boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
		/*TODO MOFICAR ESSE NEGOCIO DEPOIS
		 * */
		int frameBufferWidth = isLandscape ? 480 : 320;
		int frameBufferHeight = isLandscape ? 320 : 480;
		Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
				frameBufferHeight, Config.RGB_565); // otimização de memória

		float scaleX = (float) frameBufferWidth
				/ getWindowManager().getDefaultDisplay().getWidth();

		float scaleY = (float) frameBufferHeight
				/ getWindowManager().getDefaultDisplay().getHeight();

		renderView = new FastRenderView(this, frameBuffer);
		graphics = new CanvasGraphics(getAssets(), frameBuffer);
		fileIO = new FileIO(this);
		input = new Input(this, renderView, scaleX, scaleY);
		screen = getStartScreen();
		setContentView(renderView);

		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
				"GLGame");
	}

	@Override
	public void onResume() {
		super.onResume();
		wakeLock.acquire();
		screen.resume();
		renderView.resume();
	}

	@Override
	public void onPause() {
		super.onPause();
		wakeLock.release(); // leia o arquivo .org para saber o que é
		renderView.pause();
		screen.pause();

		if (isFinishing()) // No estado Pausado ou Stoped, o Devilk pode matar o
							// processo por qualquer motivo
			screen.dispose();
	}

	public Input getInput() {
		return input;
	}

	public FileIO getFileIO() {
		return fileIO;
	}

	public Graphics getGraphics() {
		return graphics;
	}

	/**
	 * TODO Colocar efeito de transição nessa parte
	 */
	public void setScreen(Screen screen) {
		if (screen == null)
			throw new IllegalArgumentException("Kamehamehaaaaaaaaaa");

		this.screen.pause();
		this.screen.dispose(); // elimina a Screen Anterior
		screen.resume();
		screen.update(0);
		this.screen = screen;
	}

	public Screen getCurrentScreen() {
		return screen;
	}
}
